Skip to content

feat(pre-commit): 配置 Pre-commit Hooks,实现本地 commit 前自动 lint/format 守护#187

Merged
ThreeFish-AI merged 2 commits into
feature/1.x.xfrom
vk/8389-pre-commit-hooks
Apr 17, 2026
Merged

feat(pre-commit): 配置 Pre-commit Hooks,实现本地 commit 前自动 lint/format 守护#187
ThreeFish-AI merged 2 commits into
feature/1.x.xfrom
vk/8389-pre-commit-hooks

Conversation

@ThreeFish-AI

Copy link
Copy Markdown
Owner

背景

当前项目仅在 GitHub Actions CI 中执行 ruff check / ruff format --check,lint/format 问题只能在推送后被 CI 发现,浪费 CI 资源与反馈时间。

变更内容

核心设计

采用 pre-commit 框架 + local hook(language: system + uv run 方案,而非 astral-sh/ruff-pre-commit 远程镜像仓库,理由如下:

  • Single Source of Truth:Ruff 版本由 uv.lock 唯一管控,消除版本漂移的 Split-Brain 风险
  • 与 CI 完全镜像:命令形态与 .github/workflows/ci.yml 完全一致
  • 符合 AGENTS.md 包管理规范:统一使用 uv

文件变更

文件 操作 说明
.pre-commit-config.yaml 新建 核心 hooks 配置(pre-commit-hooks 通用卫生检查 + Ruff local hooks)
pyproject.toml 修改 dev 依赖追加 pre-commit>=4.0
uv.lock 更新 锁定 pre-commit==4.5.1 及依赖
AGENTS.md 修改 卓越运营规范追加 pre-commit install 安装说明
文档/工作流文件 修复 行尾空白与 EOF 换行符(由 hooks 首次全量扫描自动修正)

Hooks 清单

trim trailing whitespace  ✓
fix end of files          ✓
check yaml (--unsafe)     ✓  # 兼容 GitHub Actions ${{ }} 语法
check toml                ✓
check merge conflicts     ✓
check added large files   ✓  # 阈值 1MB(兼容 944KB 的 assets 图片)
Ruff lint + auto-fix      ✓  # uv run ruff check --fix --exit-non-zero-on-fix
Ruff format               ✓  # uv run ruff format

验证结果

$ uv run pre-commit run --all-files
trim trailing whitespace...Passed
fix end of files...........Passed
check yaml.................Passed
check toml.................Passed
check for merge conflicts..Passed
check for added large files.Passed
Ruff (lint + auto-fix).....Passed
Ruff (format)..............Passed

安装说明(新贡献者)

uv run pre-commit install

🤖 Generated with Claude Code, CodeX, Gemini
Co-Authored-By: Aurelius Huangthreefish.ai@gmail.com

…mat 及通用代码卫生检查;

- 新建 .pre-commit-config.yaml:使用 local hook(language: system + uv run)调用 uv.lock 锁定的 Ruff,
  确保本地 hooks 与 CI 流水线使用完全一致的版本(Single Source of Truth);
  并集成 pre-commit-hooks 通用卫生检查(trailing-whitespace、end-of-file-fixer、check-yaml 等)
- pyproject.toml:dev 依赖组追加 pre-commit>=4.0
- uv.lock:同步锁定 pre-commit==4.5.1 及其依赖
- AGENTS.md:卓越运营规范追加第 5 条 Pre-commit Hooks 安装与使用说明

🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist)
Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
@ThreeFish-AI
ThreeFish-AI merged commit 19e32d5 into feature/1.x.x Apr 17, 2026
6 checks passed
@ThreeFish-AI
ThreeFish-AI deleted the vk/8389-pre-commit-hooks branch April 17, 2026 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant